#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QKeyEvent>
#include "uart.h"

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
  , ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  serial_init();
  this->setStyleSheet("background-color: rgb(0, 186, 238);");
//  timer=new QTimer(this);
//  connect(timer, &QTimer::timeout, this, &MainWindow::repaintImage);
  QImage *img=GetLogo();
  if(img!=NULL)
      ui->label_Logo->setPixmap(QPixmap::fromImage(*img).scaled(ui->label_Logo->size()));
}

MainWindow::~MainWindow()
{
  delete ui;
  qDebug()<<"close";
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
  int key=event->key();
  qDebug()<<key;
  if(key==Qt::Key_F12||key==Qt::Key_Exit)
  {
      if(ShowPreWin())
          this->hide();
  }
  else
      if(ShowWin(key,0))
          this->hide();
}
void MainWindow::hideEvent(QHideEvent *event)
{

}
void MainWindow::paintEvent(QPaintEvent *event)
{

}
void MainWindow::showEvent(QShowEvent *event)
{
#ifdef Q_PROCESSOR_X86
    QDir * imagePath=new QDir(".//image//pattern");
#else
    QDir * imagePath=new QDir("/usr/bin/image/pattern");
#endif
    QStringList filter;
    filter << "*.jpg"<< "*.png"  ;
    QList<QFileInfo> *fileInfo=new QList<QFileInfo>(imagePath->entryInfoList(filter));
    QImage* img=new QImage;
    int nPattern=GetPattern();
    if(! ( img->load(fileInfo->at(nPattern).filePath()) ) ) //加载图像
    {
        delete img;
        return;
    }
    ui->label_SG->setPixmap(QPixmap::fromImage(*img).scaled(ui->label_SG->size()));
    delete img;
//    timer->start(REFRESH_TIME);
}
void MainWindow::repaintImage()
{
/*    QPixmap pixmap;
    if(GetImage(&pixmap)<0)
        return;
    pixmap = pixmap.scaled(ui->label_SA->size());
    ui->label_SA->setAutoFillBackground(true);

    QPalette palette;
    palette.setBrush(ui->label_SA->backgroundRole(), QBrush(pixmap));
    ui->label_SA->setPalette(palette);
    ui->label_SA->repaint();
*/
}
